//
//  ZLContactTopView.m
//  EnjoyAll
//
//  Created by 王晓冰 on 2017/10/30.
//  Copyright © 2017年 张雷. All rights reserved.
//

#import "ZLContactTopView.h"


@interface ZLContactTopView ()

@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,strong)UIView *changeLineView;
@property (nonatomic,strong)UIView *bottomLineView;
@property (nonatomic,strong)UIView *showChangeLineView;
@property (nonatomic,strong)NSMutableArray *buttonArray;

@end

@implementation ZLContactTopView

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray {
    self = [super initWithFrame:frame];
    if (self) {
        
        _selectIndex = NSNotFound;
        _titleArray = titleArray;
        [self _initSubViewWithArray:titleArray];
        
    }
    
    return self;
}

- (void)_initSubViewWithArray:(NSArray *)titleArray {
    
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    _buttonArray = [[NSMutableArray alloc]init];
    NSArray *arr = @[@"duihua_i",@"tongzhi_i",@"txl_i"];
    
    for (int i=0; i<titleArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.frame = CGRectMake(i*(kSCREEN_WIDTH/titleArray.count*1.0), 0, kSCREEN_WIDTH/titleArray.count*1.0, self.height);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -20/2.0);
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5/2.0);
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:arr[i]] forState:(UIControlState)UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"454545"] forState:UIControlStateSelected];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont ZlFontOfSize:28];
        [self addSubview:button];
        [_buttonArray addObject:button];
    }
    
    _bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height-2, self.width, 2)];
    _bottomLineView.backgroundColor = [UIColor colorWithHexString:@"#dfdfdf"];
    [self addSubview:_bottomLineView];

    CGFloat width = (kSCREEN_WIDTH/titleArray.count*1.0-70)*0.5;
//    _changeLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height-2, kSCREEN_WIDTH/titleArray.count*1.0, 2)];
    _changeLineView = [[UIView alloc]initWithFrame:CGRectMake(width, self.height-2, 70, 2)];

    [self addSubview:_changeLineView];
    
    _showChangeLineView = [[UIView alloc]initWithFrame:_changeLineView.bounds];
    _showChangeLineView.backgroundColor = [UIColor colorWithHexString:@"#454545"];
    [_changeLineView addSubview:_showChangeLineView];
    
    
    [self setSelectIndex:0];
}

- (void)buttonAction:(UIButton *)button {
    self.selectIndex = button.tag;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    
    if (_selectIndex == selectIndex) {
        return;
    }
    
    _selectIndex = selectIndex;
    
    for (UIButton *button in _buttonArray) {
        button.selected = NO;
    }
    
    UIButton *selectButton = _buttonArray[_selectIndex];
    selectButton.selected = YES;
    
    [UIView animateWithDuration:0.35 animations:^{
        
        _changeLineView.left = (kSCREEN_WIDTH/_titleArray.count*1.0-70)*0.5+kSCREEN_WIDTH/_titleArray.count*_selectIndex;;
    }];
    
    if (_topBlock) {
        _topBlock(_selectIndex);
    }
}

- (void)setChangLineColor:(UIColor *)changLineColor {
    _changLineColor = changLineColor;
    _showChangeLineView.backgroundColor = _changLineColor;
}

- (void)setChangeLineWidth:(CGFloat)changeLineWidth {
    _changeLineWidth = changeLineWidth;
    _showChangeLineView.width = _changeLineWidth;
    _showChangeLineView.left = (_changeLineView.width-_changeLineWidth)*0.5;
}

- (void)setIsHiddenBottomLineView:(BOOL)isHiddenBottomLineView {
    _isHiddenBottomLineView = isHiddenBottomLineView;
    _bottomLineView.hidden = _isHiddenBottomLineView;
}






@end
